package com.richapp.launcher.scene.conditions.status;

import com.richapp.launcher.database.system.dao.ConditionSensorModelDao;
import com.richapp.launcher.database.system.dao.ConditionStatusModelDao;
import com.richapp.launcher.database.system.dao.SceneModelDao;
import com.richapp.launcher.database.system.mode.ConditionSensorModel;
import com.richapp.launcher.database.system.mode.ConditionStatusModel;
import com.richapp.launcher.database.system.mode.DeviceModel;
import com.richapp.launcher.database.system.mode.SceneModel;
import com.richapp.launcher.scene.SceneServer;
import com.richapp.launcher.scene.abstracts.SceneAbstractStatusConditions;
import com.richapp.launcher.scene.interfaces.ISceneConditions;
import com.richapp.launcher.scene.interfaces.ISceneStatusConditions;

public class SceneStatusOpenConditions extends SceneAbstractStatusConditions {

	public SceneStatusOpenConditions() {
	}

	@Override
	public int getConditionType() {
		return ISceneConditions.SCENE_CONDITION_TYPE_STATUS;
	}

	@Override
	public int getConditionSubType() {
		return ISceneStatusConditions.SCENE_CONDITION_SUB_TYPE_STATUS_OPEN;
	}

	@Override
	public boolean checkCondition(String sceneName) {
		boolean checkFlag=false;
		DeviceModel device = getDevice();
		SceneModel sceneModel = new SceneModel();
		SceneModelDao sceneModelDao = new SceneModelDao(SceneServer.getInstance().getContext());
		if (sceneName.equals("")){
			return false;
		}
		sceneModel=sceneModelDao.getSceneModelOnly(sceneName);
		if(device != null) {
			if ("17".equals(device.getDeviceId())) {
				if (device.getDeviceStatus().equals("C0") && "true".equals(sceneModel.getStatusCycleFlag())) {
					checkFlag = true;
				} else if (!device.getDeviceStatus().equals("C0")) {
					sceneModel.setStatusCycleFlag("true");
					sceneModelDao.add(sceneModel);
				}
			} else if ("0D".equals(device.getDeviceId())) {
				if (device.getDeviceStatus().equals("01") && "true".equals(sceneModel.getStatusCycleFlag())) {
					checkFlag = true;
				} else if (!device.getDeviceStatus().equals("01")) {
					sceneModel.setStatusCycleFlag("true");
					sceneModelDao.add(sceneModel);
				}
			}else if ("0E".equals(device.getDeviceId())){
				if (device.getLockKeyStatus().equals("71") && "true".equals(sceneModel.getStatusCycleFlag())
						|| device.getLockKeyStatus().equals("73") && "true".equals(sceneModel.getStatusCycleFlag())) {
					checkFlag = true;
				} else if (!device.getLockKeyStatus().equals("71") && !device.getLockKeyStatus().equals("73")) {
					sceneModel.setStatusCycleFlag("true");
					sceneModelDao.add(sceneModel);
				}
			} else {
				if (device.getDeviceStatus().equals("FF") && "true".equals(sceneModel.getStatusCycleFlag())) {
					checkFlag = true;
				} else if (!device.getDeviceStatus().equals("FF")) {
					sceneModel.setStatusCycleFlag("true");
					sceneModelDao.add(sceneModel);
				}
			}
		}
		return checkFlag;
	}

}
